#include "Light.h"

Light *Light::lights;
int Light::numberOfLights;

Vector3 Light::Position(void)
{
	return position;
}
void Light::SetPosition(Vector3 position)
{
	this->position = position;
}
	
Vector3 Light::Ambient(void)
{
	return ambient;
}
void Light::SetAmbient(Vector3 ambient)
{
	this->ambient = ambient;
}
	
Vector3 Light::Diffuse(void)
{
	return diffuse;
}
void Light::SetDiffuse(Vector3 diffuse)
{
	this->diffuse = diffuse;
}
	
Vector3 Light::Specular(void)
{
	return specular;
}
void Light::SetSpecular(Vector3 specular)
{
	this->specular = specular;
}

int Light::Index(void)
{
	return index;
}
void Light::SetIndex(int index)
{
	this->index = index;
}

void Light::Init(float x, float y, float z)
{
	float specref[] = {1, 1, 1, 1};
	glMaterialfv(GL_FRONT, GL_SPECULAR, specref);
	glMateriali(GL_FRONT, GL_SHININESS, 10);
	
	float ambient [] = {0.1f, 0.1f, 0.1f, 0};
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);

	numberOfLights = 5;

	lights = new Light[numberOfLights];
	lights[0].index = GL_LIGHT0;
	lights[0].ambient = Vector3(0.0f, 0.0f, 0.0f);
	lights[0].diffuse = Vector3(0.4f, 0.2f, 0.2f);
	lights[0].specular = Vector3(0.1f, 0.0f, 0.0f);
	lights[0].position = Vector3(x, y + 1.0f, z);
	
	lights[1].index = GL_LIGHT1;
	lights[1].ambient = Vector3(0, 0, 0);
	lights[1].diffuse = Vector3(0.3f, 0.2f, 0.3f);
	lights[1].specular = Vector3(0.0f, 0, 0.1f);
	lights[1].position = Vector3(0, y + 1.0f, 0);

	lights[2].index = GL_LIGHT2;
	lights[2].ambient = Vector3(0, 0, 0);
	lights[2].diffuse = Vector3(0.2f, 0.4f, 0.2f);
	lights[2].specular = Vector3(0.0f, 0.1f, 0.0f);
	lights[2].position = Vector3(x, y + 1.0f, 0);

	lights[3].index = GL_LIGHT3;
	lights[3].ambient = Vector3(0, 0, 0);
	lights[3].diffuse = Vector3(0.4f, 0.2f, 0.4f);
	lights[3].specular = Vector3(0.1f, 0, 0.1f);
	lights[3].position = Vector3(0, y + 1.0f, z);

	lights[4].index = GL_LIGHT3;
	lights[4].ambient = Vector3(0, 0, 0);
	lights[4].diffuse = Vector3(0.2f, 0.2f, 0.2f);
	lights[4].specular = Vector3(0.2f, 0.2f, 0.2f);
	lights[4].position = Vector3(0, 0, 0);	

	for(int i = 0; i < numberOfLights; i++)
		lights[i].Update();
}

void Light::Clear(void)
{
	delete[] lights;
	lights = NULL;
}

void Light::Update(void)
{
	float f[4];
	f[3] = 1;	
	f[0] = (float)ambient.x;
	f[1] = (float)ambient.y;
	f[2] = (float)ambient.z;
	glLightfv(index, GL_AMBIENT, f);	
	f[0] = (float)diffuse.x;	
	f[1] = (float)diffuse.y;
	f[2] = (float)diffuse.z;
	glLightfv(index, GL_DIFFUSE, f);	
	f[0] = (float)specular.x;	
	f[1] = (float)specular.y;
	f[2] = (float)specular.z;
	glLightfv(index, GL_SPECULAR, f);	
	f[0] = (float)position.x;	
	f[1] = (float)position.y;
	f[2] = (float)position.z;
	glLightfv(index, GL_POSITION, f);
}

void Light::LightsOn(void)
{
	for(int i = 0; i < numberOfLights; i++)
		glEnable(lights[i].index);
}
void Light::LightOff(void)
{
	for(int i = 0; i < numberOfLights; i++)
		glDisable(lights[i].index);
}

void Light::UpdateAll(void)
{
	for(int i = 0; i < numberOfLights; i++)
		lights[i].Update();
}

Light* Light::GetLight(int index)
{
	if(index >= numberOfLights)
		return NULL;
	return &lights[index];
}